package com.itany.book.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.mapper.BookMapper;
import com.itany.mapper.ExamineMapper;
import com.itany.pojo.Book;
import com.itany.pojo.Examine;
import com.itany.service.ExamineService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Description: 类的描述
 * Author: SJY
 * Date: 2021/08/31 15:25
 * Version: 1.0 版本信息
 */
@Service
@Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRED)
public class ExamineServiceImpl implements ExamineService {

    @Autowired
    ExamineMapper examineMapper;
    @Autowired
    BookMapper bookMapper;

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public PageInfo<Examine> selectExamine(Integer page, Integer rows,Integer supplierid) {
        PageHelper.startPage(page, rows);
        List<Examine> list = examineMapper.selectExamine(supplierid);
        PageInfo<Examine> info = new PageInfo<Examine>(list);
        return info;
    }

    public ActionResult insertExamine(Book book,Integer supplierid) {
        ActionResult ar = new ActionResult();
        try{
            Integer ISBN = book.getISBN();
            Integer bookid = null;
            bookid = bookMapper.selectISBN(ISBN);
            if(bookid==null){
                bookid = bookMapper.insertBook(book);
            }
            Examine examine = new Examine();
            examine.setBookid(bookid);
            examine.setNumber(book.getNumber());
            examine.setInfo(book.getBookname()+"审核");
            examine.setSupplierid(supplierid);
            examine.setTitle(book.getBookname()+"审核");
            examine.setFlag(0);
            Integer id = examineMapper.insertExamine(examine);
            ar.setMsg("添加成功");
            ar.setData(id);
            ar.setStatus(1);
        }catch (Exception e){
            e.printStackTrace();
            ar.setMsg("服务器内部异常");
            ar.setStatus(0);
        }finally {
            return ar;
        }
    }
}
